﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Zzj.Viewmodel;

namespace Zzj.Windows
{
    /// <summary>
    /// InputWindow.xaml 的交互逻辑
    /// </summary>
    public partial class InputWindow : Window
    {
        public InputViewModel viewModel;
        public int maxLength = 0;
        public List<String> words = new List<string>(0);
        private Action<int, String> callbackAction;

        public InputWindow()
        {
            InitializeComponent();
            this.Loaded += Win_Load;

            viewModel = new InputViewModel();
            viewModel.index = 0;
            this.DataContext = viewModel;
        }

        public InputWindow(Action<int, String> callbackAction)
        {
            InitializeComponent();
            this.Loaded += Win_Load;

            viewModel = new InputViewModel();
            viewModel.index = 0;
            this.DataContext = viewModel;
            this.callbackAction = callbackAction;
        }

        private void Win_Load(object sender, RoutedEventArgs e)
        {
            UIElementCollection buttons01 = keyPanel01.Children;
            foreach (UIElement element in buttons01)
            {
                if (element is Button)
                {
                    Button keyButton = (element as Button);
                    keyButton.Click += KeyButton_Click_01;
                }
            }

            UIElementCollection buttons02 = keyPanel02.Children;
            foreach (UIElement element in buttons02)
            {
                if (element is Button)
                {
                    Button keyButton = (element as Button);
                    keyButton.Click += KeyButton_Click_02;
                }
            }
        }

        public void showKeys(int idx)
        {
            if (idx == 1)
            {
                keyPanel01.Visibility = Visibility.Visible;
                keyPanel02.Visibility = Visibility.Hidden;
            }
            else
            {
                keyPanel01.Visibility = Visibility.Hidden;
                keyPanel02.Visibility = Visibility.Visible;
            }
        }

        private void KeyButton_Click_01(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            String val = (String)button.Content;
            viewModel.carNoArray.Clear();
            viewModel.carNoArray.Add(val);
            showKeys(2);
            if (callbackAction != null)
            {
                callbackAction.Invoke(viewModel.carNoArray.Count - 1, val);
            }
        }

        private void KeyButton_Click_02(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            String val = (String)button.Content;
            if (val == "删除")
            {
                if (viewModel.carNoArray.Count > 0)
                {
                    viewModel.carNoArray.RemoveAt(viewModel.carNoArray.Count - 1);
                    if (callbackAction != null)
                    {
                        callbackAction.Invoke(viewModel.carNoArray.Count, "");
                    }
                }
                if (viewModel.carNoArray.Count == 0)
                {
                    showKeys(1);
                    callbackAction.Invoke(0, "");
                }
            }
            else
            {
                if (viewModel.carNoArray.Count < maxLength)
                {
                    viewModel.carNoArray.Add(val);
                    callbackAction.Invoke(viewModel.carNoArray.Count - 1, val);
                }
            }
        }

        private void hide_keywin(object sender, MouseButtonEventArgs e)
        {
            this.Hide();
        }
    }
}